package ua.com.infinite.mobile;

import org.json.JSONException;
import org.json.JSONObject;

import ua.com.infinite.mobile.db.DBAdapter;
import ua.com.infinite.mobile.plains.User;
import ua.com.infinite.mobile.server.Constants;
import ua.com.infinite.mobile.server.DataLoaderBase;
import ua.com.infinite.mobile.server.DataLoaderLoginJoin;
import ua.com.infinite.mobile.server.OnDownloadingListener;
import ua.com.infinite.mobile.server.RequestObject;
import ua.com.infinite.mobile.utils.PhoneUtils;
import ua.com.infinite.mobile.utils.SharedPreferencesUtils;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class LoginActivity extends FragmentActivity implements OnDownloadingListener, OnClickListener {

	public static final int DIALOG_LOGIN = 101;

	private TextView logo;
	private EditText loginEd;
	private EditText passwordEd;
	private Button btnLogin;
	private Button btnRegister;
	private Button btnMain;

	private ProgressDialog progressDialog;
	private int showedDialogId;

	private String lastAction;

	private String password;
	private String login;

	private DataLoaderLoginJoin dataLoader;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		setupUI();

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			login = data.getStringExtra(Constants.USER_LOGIN_LOGIN);
			password = data.getStringExtra(Constants.USER_LOGIN_PASSWORD);
			loginEd.setText(login);
			passwordEd.setText(password);
		} else {
			loginEd.setText("");
			passwordEd.setText("");
		}
	}

	@Override
	public Dialog onCreateDialog(int id) {

		switch (id) {
		case DIALOG_LOGIN:
			progressDialog = new ProgressDialog(LoginActivity.this);
			progressDialog.setMessage(getResources().getText(R.string.entering));
			progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			return progressDialog;
		default:
			return null;
		}

	}

	public void setupUI() {
		Typeface frederickaFont = Typeface.createFromAsset(getAssets(), "FrederickatheGreat.ttf");
		logo = (TextView) findViewById(R.id.logo);
		logo.setTypeface(frederickaFont);

		loginEd = (EditText) findViewById(R.id.loginEd);
		passwordEd = (EditText) findViewById(R.id.passwordEd);
		btnLogin = (Button) findViewById(R.id.btn_login);
		btnLogin.setOnClickListener(this);
		btnRegister = (Button) findViewById(R.id.btn_registrate);
		btnRegister.setOnClickListener(this);

		btnMain = (Button) findViewById(R.id.btn_open_main);
		btnMain.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_login:
			login = loginEd.getText().toString();
			password = passwordEd.getText().toString();
			if (login.equals("")) {
				break;
			} else if (password.equals("")) {
				break;
			} else {
				RequestObject requestObject = new RequestObject();
				requestObject.setAction(Constants.LOGIN);
				requestObject.addParam(Constants.USER_LOGIN_LOGIN, login);
				requestObject.addParam(Constants.USER_LOGIN_PASSWORD, password);
				startDownload(requestObject, true);
			}
			break;
		case R.id.btn_registrate:
			Intent registrateIntent = new Intent(LoginActivity.this, JoinActivity.class);
			startActivityForResult(registrateIntent, 1);
			break;

		case R.id.btn_open_main:
			Intent intent = new Intent(LoginActivity.this, MainActivity.class);
			intent.putExtra(Constants.USER_FIRST_NAME, "First");
			intent.putExtra(Constants.USER_LAST_NAME, "Last");
			startActivity(intent);
			break;

		default:
			break;
		}

	}

	@Override
	public void startDownload(RequestObject request, boolean returnDataToCaller) {
		lastAction = request.getAction();
		dataLoader = new DataLoaderLoginJoin(LoginActivity.this, this, returnDataToCaller);
		dataLoader.execute(new RequestObject[] { request });
	}

	@Override
	public void onStartDownloading() {
		showDialog(DIALOG_LOGIN);
		showedDialogId = DIALOG_LOGIN;
	}

	@Override
	public void succesDownloading(boolean isSucces) {
		progressDialog.dismiss();
	}

	@Override
	public void returnData(JSONObject object) {
		if (PhoneUtils.isJSonValid(object)) {
			App.logIn(getApplicationContext(), object);
			LoginActivity.this.setResult(RESULT_OK);
			LoginActivity.this.finish();
		}

	}

}
